package com.igoal.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Will be replace by spring security oauth
 * 
 * 
 */
public class OAuthFilter implements Filter {

	private static final String PROVIDER_BASE_URL = ".providerBaseUrl";
	private static final String REQUEST_TOKEN_URL = ".requestTokenUrl";
	private static final String USER_AUTHORIZATION_URL = ".userAuthorizationUrl";
	private static final String ACCESS_TOKEN_URL = ".accessTokenUrl";
	private static final String REQUEST_SCOPE = ".requestScope";
	private static final String CONSUMER_KEY = ".consumerKey";
	private static final String CONSUMER_SECRET = ".consumerSecret";
	private static final String SIGNATURE_METHOD = ".signatureMethod";

	public static final String REQUEST_TOKEN = ".requestToken";
	public static final String ACCESS_TOKEN = ".accessToken";
	public static final String TOKEN_SECRET = ".tokenSecret";

	private FilterConfig filterConfig = null;

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		// HttpServletResponse response = (HttpServletResponse) resp;
		// HttpServletRequest request = (HttpServletRequest) req;
		// String callbackUrl = request.getRequestURL().toString();
		//
		// OAuthConsumer consumer = null;
		// String consumerName = null;
		//
		// if (callbackUrl.indexOf("yahoo") > -1) {
		// consumerName = "yahoo";
		// } else {
		// consumerName = "google";
		// }
		//
		// OAuthServiceProvider serviceProvider = new OAuthServiceProvider(
		// filterConfig.getInitParameter(consumerName + PROVIDER_BASE_URL),
		// filterConfig.getInitParameter(consumerName
		// + USER_AUTHORIZATION_URL), filterConfig
		// .getInitParameter(consumerName + ACCESS_TOKEN_URL));
		// consumer = new OAuthConsumer(callbackUrl,
		// filterConfig.getInitParameter(consumerName + CONSUMER_KEY),
		// filterConfig.getInitParameter(consumerName + CONSUMER_SECRET),
		// serviceProvider);
		//
		// // create accessor
		// OAuthAccessor accessor = getAccessor(request, consumer,
		// consumerName);

		// if (accessor.accessToken == null) {
		// getAccessToken(request, accessor, consumerName);
		// }

		// boolean authFlag = false;
		//
		// OAuth3LeggedScheme scheme = null;
		// if (session != null) {
		// scheme = (OAuth3LeggedScheme) session.getAttribute(SCHEME_KEY);
		// }
		//
		// if (scheme == null) {
		// authFlag = true;
		// } else {
		// if (scheme.getAccessToken() != null) {
		// chain.doFilter(req, resp);
		// } else if (scheme.getRequestToken() != null && tokenParam != null) {
		// try {
		// scheme.requestAccessToken(tokenParam, verifierParam);
		// session.setAttribute(SCHEME_KEY, scheme);
		// response.sendRedirect(request.getRequestURL().toString());
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// } else {
		// authFlag = true;
		// }
		// }
		//
		// if (authFlag) {
		// String callbackUrl = request.getRequestURL().toString();
		// if (callbackUrl.indexOf("yahoo") > -1) {
		// scheme = new OAuth3LeggedScheme(new YahooProvider(), YAHOO_KEY,
		// YAHOO_SECRET);
		// } else if (callbackUrl.indexOf("google") > -1) {
		// scheme = new OAuth3LeggedScheme(new GoogleProvider(),
		// GOOGLE_KEY, GOOGLE_SECRET);
		// } else {
		// chain.doFilter(req, resp);
		// }
		//
		// try {
		// String authUrl = scheme.getAuthorizationUrl(callbackUrl);
		// session.setAttribute(SCHEME_KEY, scheme);
		// response.sendRedirect(authUrl);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
	}

	public void destroy() {
	}

	// private OAuthAccessor getAccessor(HttpServletRequest request,
	// OAuthConsumer consumer, String consumerName) {
	// CookieMap cookies = new CookieMap(request);
	// OAuthAccessor accessor = new OAuthAccessor(consumer);
	// accessor.requestToken = cookies.get(consumerName + ".requestToken");
	// accessor.accessToken = cookies.get(consumerName + ".accessToken");
	// accessor.tokenSecret = cookies.get(consumerName + ".tokenSecret");
	//
	// if (accessor.accessToken == null) {
	// // getAccessToken(request, cookies, accessor);
	// }
	//
	// return accessor;
	// }

	// private void getAccessToken(HttpServletRequest request,
	// OAuthAccessor accessor, String consumerName) throws OAuthException,
	// IOException, URISyntaxException {
	// HttpSession session = request.getSession();
	// final String callbackURL = getCallbackURL(request, consumerName);
	// List<OAuth.Parameter> parameters = OAuth.newList(OAuth.OAUTH_CALLBACK,
	// callbackURL);
	//
	// if (consumerName.equals("google")) {
	// parameters.add(new OAuth.Parameter("scope", filterConfig
	// .getInitParameter(consumerName + REQUEST_SCOPE)));
	// }
	//
	// OAuthMessage response = CLIENT.getRequestTokenResponse(accessor, null,
	// parameters);
	//
	// session.setAttribute(consumerName + ".requestToken",
	// accessor.requestToken);
	// session.setAttribute(consumerName + ".tokenSecret",
	// accessor.tokenSecret);
	//
	// String authorizationURL =
	// accessor.consumer.serviceProvider.userAuthorizationURL;
	// if (authorizationURL.startsWith("/")) {
	// authorizationURL = (new URL(new URL(request.getRequestURL()
	// .toString()), request.getContextPath() + authorizationURL))
	// .toString();
	// }
	//
	// authorizationURL = OAuth.addParameters(authorizationURL //
	// , OAuth.OAUTH_TOKEN, accessor.requestToken);
	// if (response.getParameter(OAuth.OAUTH_CALLBACK_CONFIRMED) == null) {
	// authorizationURL = OAuth.addParameters(authorizationURL //
	// , OAuth.OAUTH_CALLBACK, callbackURL);
	// }
	//
	// // throw new RedirectException(authorizationURL);
	// }
	//
	// private String getCallbackURL(HttpServletRequest request,
	// String consumerName) throws IOException {
	// URL base = new URL(new URL(request.getRequestURL().toString()), //
	// request.getContextPath() + Callback.PATH);
	// return OAuth.addParameters(base.toExternalForm() //
	// , "consumer", consumerName //
	// , "returnTo", getRequestPath(request) //
	// );
	// }

}
